package com.parkingspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.PersonWalletMapper;
import com.parkingspace.dao.UserMapper;
import com.parkingspace.dao.WalletUserMapper;
import com.parkingspace.entity.PersonWallet;
import com.parkingspace.entity.User;
import com.parkingspace.entity.WalletUser;
import com.parkingspace.service.IPersonWalletService;
import com.parkingspace.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 个人钱包表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
@Slf4j
public class PersonWalletServiceImpl extends ServiceImpl<PersonWalletMapper, PersonWallet> implements IPersonWalletService {

	@Resource
	private UserMapper userMapper;

	@Resource
	private WalletUserMapper walletUserMapper;

	@Resource
	private PersonWalletMapper personWalletMapper;

	@Resource
	private RedisTemplate<String, Object> stringRedisTemplate;

	@Override
	public Double queryWalletAmount(String token, Integer role) throws Exception {
		String username = JwtUtils.getUsername(token);
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("uname", username);
		User user = userMapper.selectOne(queryWrapper);
		if (user == null) {
			throw new RuntimeException("查询钱包金额异常!");
		}
		QueryWrapper<WalletUser> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("uid", user.getUid());
		queryWrapper1.eq("rid", role);
		WalletUser walletUser = walletUserMapper.selectOne(queryWrapper1);
		if (walletUser == null) {
			throw new RuntimeException("查询钱包金额异常!");
		}
		QueryWrapper<PersonWallet> queryWrapper2 = new QueryWrapper<>();
		queryWrapper2.eq("wallet_Id", walletUser.getWalletId());
		PersonWallet personWallet = personWalletMapper.selectOne(queryWrapper2);
		if (personWallet == null) {
			throw new RuntimeException("查询钱包金额异常!");
		}
		return personWallet.getMoney();
	}

	@Override
	public Boolean walletRecharge(String token, Double money) throws Exception {
		String username = JwtUtils.getUsername(token);
		String rid = (String) stringRedisTemplate.opsForValue().get(username);
		log.info("rid为:{}",rid);
		QueryWrapper<User> q = new QueryWrapper<>();
		q.eq("uname",username);
		User user = userMapper.selectOne(q);
		System.out.println(rid+","+user.getUid()+"-----------------------------------------------");
		if (rid == null || user.getUid() == null) {
			throw new RuntimeException("请先进行登录!");
		}
		QueryWrapper<WalletUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("uid",user.getUid());
		queryWrapper.eq("rid",rid);
		WalletUser walletUser = walletUserMapper.selectOne(queryWrapper);
		if (walletUser == null) {
			throw new RuntimeException("没有查询到该用户的钱包信息!");
		}
		PersonWallet personWallet = personWalletMapper.selectById(walletUser.getWalletId());
		if (personWallet == null) {
			throw new RuntimeException("没有查询到该用户的钱包信息!");
		}
		double v = personWallet.getMoney() + money;
		personWallet.setMoney(v);
		QueryWrapper<PersonWallet> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("wallet_Id", personWallet.getWalletId());
		boolean b = personWalletMapper.update(personWallet, queryWrapper1) == 1;

		return b;
	}
}
